//引入字体图标样式
require("../../assets/fonts/iconfont.css");
const axios = require("../../assets/js/axios.js");
//引入自定义js
let obj = require("../../assets/js/my.js");
//引入自己的样式
require("./mine_information.less");
let $ = obj.$;
let local = obj.local;
let padZero = obj.padZero;
document.addEventListener("DOMContentLoaded", async function () {
	//1.点击箭头跳转
	$("#skip").addEventListener("click", function () {
		history.back();
	});
	//2.性别
	$("#gender").addEventListener("click", function () {
		weui.picker(
			[
				{
					label: "男",
					value: 0,
				},
				{
					label: "女",
					value: 1,
				},
				{
					label: "未知",
					value: 2,
				},
			],
			{
				onConfirm: function (result) {
					$("#gender").textContent = result[0].label;
					$("#gender").dataset.value = result[0].value;
				},
				title: "性别",
				showClose: false,
			}
		);
	});
	//3.生日
	$("#birthday").addEventListener("click", function () {
		weui.datePicker({
			start: 1910,
			end: new Date().getFullYear(),

			onConfirm: function (result) {
				$("#birthday").textContent = result
					.map((v) => padZero(v.value))
					.join("-");
			},
			title: "生日",
		});
	});
	//4.头像上传
	$("#file").addEventListener("change", function () {
		//实例化对象
		const fd = new FormData();
		fd.append("file", this.files[0]);
		// 发送axios请求
		axios.post("/api/shared/uploadPortrait", fd).then((res) => {
			if (res.data.errno === 0) {
				$("#adator").src = axios.defaults.baseURL + res.data.data.url;
				$("#adator").dataset.value = res.data.data.url;
			} else {
				//上传失败
				alert("上传失败");
			}
		});
	});
	//5.省市二级联动
	//省联动
	let proviceId = 0;
	let pro = await axios.get("api/shared/province");
	// console.log(pro);
	$("#provice").addEventListener("click", async function () {
		//获取数据
		if (pro.data.errno !== 0) return;
		let proArr = pro.data.data.map((v) => ({ label: v.name, value: v.id }));
		proviceId = await new Promise((resolve) => {
			weui.picker(proArr, {
				onConfirm: function (result) {
					$("#provice").textContent = result[0].label;
					$("#provice").dataset.value = result[0].value;
					proviceId = result[0].value;
					resolve(proviceId);
				},
				title: "省份",
			});
		});
		let city = await axios.get("/api/shared/city?provinceId=" + proviceId);
		//获取数据
		let cityArr = city.data.data.map((v) => ({ label: v.name, value: v.id }));
		weui.picker(cityArr, {
			onConfirm: function (result) {
				$("#city").textContent = result[0].label;
				$("#city").dataset.value = result[0].value;
			},
			title: "省份",
		});
	});
	//6.城市
	// $("#city").addEventListener("click", async function () {
	// 	let city = await axios.get("/api/shared/city?provinceId=" + proviceId);
	// 	if (!proviceId) {
	// 		$("#textToast").style.opacity = "1";
	// 		$("#textToast").style.display = "block";
	// 		setTimeout(function () {
	// 			$("#textToast").style.opacity = "0";
	// 			$("#textToast").style.display = "none";
	// 		}, 2000);
	// 		return;
	// 	}
	// 	//获取数据
	// 	let cityArr = city.data.data.map((v) => ({ label: v.name, value: v.id }));
	// 	weui.picker(cityArr, {
	// 		onConfirm: function (result) {
	// 			$("#city").textContent = result[0].label;
	// 			$("#city").dataset.value = result[0].value;
	// 		},
	// 		title: "省份",
	// 	});
	// });
	//7.个性签名
	$("#sign").addEventListener("input", function () {
		$("#textNum").textContent = this.value.length;
	});
	//8.信息上传保存
	$("#btn").addEventListener("click", function () {
		//信息
		let nickName = $("#nickName").value;
		let gender = Number($("#gender").dataset.value);
		let provinceId = Number($("#provice").dataset.value);
		let cityId = Number($("#city").dataset.value);
		let birthday = $("#birthday").textContent;
		let imgUrl = $("#adator").dataset.value;
		let sign = $("#sign").value;
		//发送请求保存数据
		axios
			.post("/api/user/changeInfo", {
				nickName,
				gender,
				provinceId,
				cityId,
				birthday,
				imgUrl,
				sign,
			})
			.then((res) => {
				if (res.data.errno === 0) {
					local.setItem("token", res.data.data.token);
					history.back();
					alert("修改成功");
				} else {
					alert("修改失败");
				}
			});
	});
	// 性别数组
	//9.信息回填
	axios.get("/api/user/info").then(async (res) => {
		if (res.data.errno === 0) {
			let data = res.data.data;
			// 头像
			$("#adator").src = axios.defaults.baseURL + data.imgUrl;
			$("#adator").dataset.value = data.imgUrl;

			//昵称
			$("#nickName").value = data.nickName;
			//性别
			let genderArr = ["男", "女", "保密"];
			$("#gender").textContent = genderArr[data.gender];
			$("#gender").dataset.value = data.gender || 0;

			//省
			let [pross] = pro.data.data.filter((v) => v.id === data.provinceId);
			$("#provice").textContent = pross.name;
			$("#provice").dataset.value = data.provinceId;

			//城市
			let city = await axios.get(
				"/api/shared/city?provinceId=" + data.provinceId
			);
			let [city1] = city.data.data.filter((v) => v.id === data.cityId);
			$("#city").textContent = city1.name;
			$("#city").dataset.value = data.cityId;

			//生日
			$("#birthday").textContent = data.birthday;

			//简介

			$("#sign").value = data.sign;
			$("#textNum").textContent = $("#sign").value.length;
		}
	});
	//10.错误提示
});
